import pyaudio
import numpy as np
import sounddevice as sd

class AudioOutput:
    def __init__(self, sample_rate=16000):
        self.sample_rate = sample_rate
        self.p = pyaudio.PyAudio()
        
    def play(self, audio_data):
        """播放音频数据"""
        try:
            # 如果音频数据是numpy数组，转换为bytes
            if isinstance(audio_data, np.ndarray):
                audio_data = audio_data.tobytes()
                
            # 使用sounddevice播放音频
            sd.play(audio_data, self.sample_rate)
            sd.wait()
            
        except Exception as e:
            print(f"音频播放错误: {e}")